<?php

namespace ManiaLivePlugins\MLEPP\Votes\Gui\Windows;

use ManiaLive\PluginHandler\PluginHandler;
use ManiaLib\Gui\Elements\BgsPlayerCard;
use ManiaLib\Gui\Elements\Bgs1InRace;

use ManiaLive\Gui\Windowing\Controls\Pager;
use ManiaLib\Gui\Layouts\Flow;
use ManiaLib\Gui\Elements\Quad;
use ManiaLib\Gui\Elements\Label;
use ManiaLive\Gui\Windowing\Controls\Frame;
use ManiaLivePlugins\MLEPP\Core\Core;
use ManiaLib\Gui\Elements\Button;
use ManiaLib\Gui\Elements\Format;
use ManiaLivePlugins\MLEPP\Votes\Votes;
use ManiaLive\Data\Storage;

class ProgressPanel extends \ManiaLive\Gui\Windowing\Window
{
	private $frame;
	protected $line;
	protected $vote;

	private $text;
	private $background;
	private $bar_yes;
	private $bar_no;
	private $lbl_yes;
	private $lbl_no;

	function initializeComponents()
	{
            if (empty(Core::$widget_background)) {
			$this->background = new \ManiaLib\Gui\Elements\Icons128x128_Blink();
			$this->background->setSubStyle("ShareBlink");
            } else  {
                $this->background = new Quad();
		$this->background->setImage(Core::$widget_background, true);
            }
		$this->background->setSize($this->sizeX, $this->sizeY);
		$this->background->setPosY(0);
                $this->background->setValign("center");
		$this->background->setHalign("center");


		$this->frame = new Frame();
		$this->frame->setSize((0.8*$this->sizeX), (0.8*$this->sizeY));
		$this->frame->setPosY(5.5);
		$this->frame->setPosX(6);
		$this->frame->setHalign("center");


/*		$this->frame2 = new Frame();
		$this->frame2->applyLayout();
		$this->frame2->setSize($this->sizeX, $this->sizeY);
		$this->frame2->setPosY(0);
		$this->frame2->setValign("top");
		$this->frame2->setHalign("center");
		$this->addComponent($this->frame2);
	*/

	}

	function onLoad() {}

	function onDraw() {
                $this->clearComponents();
		$this->addComponent($this->background);
		$this->addComponent($this->frame);
		$stor = Storage::GetInstance();

		$yes = $this->vote->yes;
		$no = $this->vote->no;
		$total = count($stor->players);

		$yesRatio = ($yes / ($no + $total) ) * 100;
		$noRatio = ($no / ($yes + $total) ) * 100;

		$yesSize = ($yesRatio / 100) * (0.8*$this->sizeX);
		$noSize = ($noRatio / 100) * (0.8*$this->sizeX);

		$this->frame->clearComponents();

		$vote = new Label();
		$vote->setSize(30, 3);
		$vote->setPosition(($this->sizeX / 2)-6, -2);
                $vote->setPosY(-2);
		$vote->setHalign("center");
		$vote->setText('$z$s$fff'.$this->vote->vote);
                $vote->setTextSize(5);
		$this->frame->addComponent($vote);

		$btn = new Quad();
		//$btn->setImage(Core::$widget_votebar_yes,true);
		$btn->setStyle("BgRaceScore2");
		$btn->setSubStyle("CupFinisher");
		$btn->setPosition(0,2.5);
		$btn->setSize($yesSize,1.5);
		$this->frame->addComponent($btn);

		$btn = new Quad();
		//$btn->setImage(Core::$widget_votebar_no,true);
		$btn->setStyle("BgRaceScore2");
		$btn->setSubStyle("CupPotentialFinisher");
		$btn->setPosition(0,4.5);
		$btn->setSize($noSize,1.5);
		$this->frame->addComponent($btn);
		if ($yes != 0) {
		$lyes = new Label();
		$lyes->setSize(10, 1.5);
		$lyes->setPosition($yesSize,2.5);
		$lyes->setScale(0.7);
		$lyes->setHalign("right");
		$lyes->setText('$z$s$fff'.round($yesRatio,1)." [".$yes."]");
		$this->frame->addComponent($lyes);
		}

		if ($no != 0) {
		$lno = new Label();
		$lno->setSize(10, 1.5);
		$lno->setScale(0.7);
		$lno->setPosition($noSize,4.5);
		$lno->setHalign("right");
		$lno->setText('$z$s$fff'.round($noRatio,1)." [".$no."]");
		$this->frame->addComponent($lno);
				}
	}

	function onResize()
	{
		$this->frame->setSize($this->sizeX, $this->sizeY);
		$this->background->setSize($this->sizeX, $this->sizeY);

	}

	function setVote($vote) {
		$this->vote = $vote;
	}

	function addItem($item)
	{
		$this->frame->addComponent($item);
	}

	function clearItems()
	{
		$this->frame->clearComponents();
	}

	function destroy()
	{
		unset($this->vote);
		parent::destroy();
	}
}
?>